package com.skycong.hello77.refresh;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.aspsine.swipetoloadlayout.SwipeRefreshTrigger;
import com.aspsine.swipetoloadlayout.SwipeTrigger;
import com.skycong.hello77.R;

public class RefreshHeaderView extends RelativeLayout implements SwipeRefreshTrigger,SwipeTrigger {
    private ImageView ivRefresh;
    private TextView tvRefresh;
    private AnimationDrawable mAnimDrawable;
    private int mHeaderHeight;



    public RefreshHeaderView(Context context) {
        this(context,null);
    }

    public RefreshHeaderView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(getContext()).inflate(R.layout.refresh_header,this,true);
        mHeaderHeight = getResources().getDimensionPixelOffset(R.dimen.refresh_header_height);//得到高度
    }
     //进行一些初始化的操作
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ivRefresh = (ImageView) findViewById(R.id.ivRefresh);
        tvRefresh = (TextView) findViewById(R.id.tvRefresh);
        mAnimDrawable = (AnimationDrawable) ivRefresh.getDrawable();
        ivRefresh.setScaleType(ImageView.ScaleType.MATRIX);
    }

    @Override
    public void onRefresh() {
        if(!mAnimDrawable.isRunning()){
            mAnimDrawable.start();
        }
        tvRefresh.setText(R.string.refresh_more_ing);

    }

    @Override
    public void onPrepare() {

    }

    @Override
    public void onSwipe(int y, boolean b) {
        if(y>mHeaderHeight){
            tvRefresh.setText(R.string.refresh_more_release);
        }else if(y<=mHeaderHeight){}

        tvRefresh.setText(R.string.load_more_pull_up);
    }

    @Override
    public void onRelease() {
        if(!mAnimDrawable.isRunning()){
            mAnimDrawable.start();
        }

    }

    @Override
    public void complete() {
        tvRefresh.setText(R.string.refresh_more_complete);

    }

    @Override
    public void onReset() {
        mAnimDrawable.stop();

    }
}
